#include "setwig.h"
#include "ui_setwig.h"

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    ui->setupUi(this);




    connect(ui->pushButton_2new,SIGNAL(clicked()),this,SLOT(dataDownSlot()));
    connect(ui->pushButton_3tem,SIGNAL(clicked()),this,SLOT(dataDownSlot()));
    connect(ui->pushButton_3tem_down,SIGNAL(clicked()),this,SLOT(dataDownSlot()));
    connect(ui->pushButton_4_hum,SIGNAL(clicked()),this,SLOT(dataDownSlot()));


}

SetWig::~SetWig()
{
    delete ui;

}


void SetWig::dataDownSlot()
{
    QJsonObject address;
    QJsonObject data;

    if(ui->pushButton_2new == sender())
    {
        qDebug() << "刷新方式" << ui->comboBox->currentText();
        address.insert("type",3);
        data.insert("period", 5);
        //设置上报类型
        if(ui->comboBox->currentText()=="手动上报")
        {data.insert("type", 0);
            qDebug()<<"手动上报";}
        if(ui->comboBox->currentText()=="变化上报")
        {data.insert("type", 1);
            qDebug()<<"变化上报";}
        if(ui->comboBox->currentText()=="周期上报")
        {data.insert("type", 2);
            qDebug()<<"周期上报";}
    }else if (ui->pushButton_3tem== sender())
    {
        qDebug() << "温度阈值上"<<ui->spinBox_tem_up->text() ;
        address.insert("type",2);
        data.insert("key", 305);
        data.insert("val", ui->spinBox_tem_up->text());
    }else if (ui->pushButton_3tem_down==sender())
    {
        qDebug() << "温度阈值下"<< ui->spinBox_2_tem_down->text();
        address.insert("type",2);
        data.insert("key", 306);


        data.insert("val", ui->spinBox_2_tem_down->text());
    }
    else if (ui->pushButton_4_hum==sender())
    {
        qDebug() << "湿度阈值下";
        address.insert("type",2);
        data.insert("key", 307);
        data.insert("val", ui->spinBox_3_hum->text());

    }
    address.insert("data",QJsonValue(data));
    // 将QJsonObject转换为QJsonDocument
    QJsonDocument jsonDoc(address);
    // 输出JSON字符串
    QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
    qDebug() << "Serialized JSON:" << jsonString;
    // 创建一个QMQTT::Message对象
    quint16 idd = 1;
    QString topic = "/app/data/down";
    QByteArray payload = jsonString.toUtf8();
    //    QByteArray payload = jsonDoc.toBinaryData();
    quint8 qos = 1;
    bool retain = false;
    QMQTT::Message message(idd, topic, payload, qos, retain);
    GlobalVal::mqtt->publish( message);

}
